package com.formula.propertyshop.view.phone;

import java.util.List;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;

import com.formula.propertyshop.R;
import com.formula.propertyshop.adapter.PropertyGirdViewAdapter;
import com.formula.propertyshop.model.Property;
import com.formula.propertyshop.service.IWireFrameService;
import com.formula.propertyshop.service.WireFrameServiceFactory;
import com.formula.propertyshop.utils.Const;
import com.formula.propertyshop.view.common.BaseActivity;

public class SearchResultActivity extends BaseActivity{
	private GridView gView;
	private PropertyGirdViewAdapter adapter;
	private boolean loadingMore = true;
	private int page=1;
	private TextView error;
	
	private String url;
	@Override
	protected void initUI() {
		setContentView(R.layout.searchresult);
		gView = (GridView) findViewById(R.id.gridPropety);
		error = (TextView) findViewById(R.id.search_error);
		TextView title = (TextView) findViewById(R.id.txtTitle);
		title.setText(getString(R.string.search_result));
		setControls();
		loadData();
	}
	private void loadData() {
		showProgressDlg();
		url = getIntent().getStringExtra(Const.PROPERTYID);
		gView.setEnabled(true);
		
		Thread thread = new Thread(new Runnable() {
			@Override
			public void run() {
				loadingMore=true;
				Message msg = handlerSearch.obtainMessage();
				IWireFrameService service = WireFrameServiceFactory.getInstance().getService();
				List<Property> properties = null;
				String urlSearch = url+page+"&itemsperpage=10";
				try {
					properties = service.getSearchProperty(urlSearch);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				
				}
				
				msg.obj = properties;
				handlerSearch.sendMessage(msg);
			}
		});
		thread.start();
		
		//handleSearch();
	}
	private void setControls(){
		adapter = new PropertyGirdViewAdapter(this);
		gView.setAdapter(adapter);
		gView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Property p = (Property) adapter.getItem(arg2);
				Intent intent = new Intent(SearchResultActivity.this,PropertyDetailActivity.class);
				intent.putExtra(Const.PROPERTYID, p.getId());
				startActivity(intent);
			}
		});
		gView.setOnScrollListener(new AbsListView.OnScrollListener() {
			
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				int lastInScreen = firstVisibleItem + visibleItemCount;
				
				Log.d("firstVisibleItem",firstVisibleItem+"");
				Log.d("lastInScreen", lastInScreen+"");
				Log.d("totalItemCount",totalItemCount+"");
				Log.d("loadingMore",loadingMore+"");
				if((lastInScreen == totalItemCount) && !(loadingMore)){
					Thread thread =  new Thread(null, loadMoreProperty);
					thread.start();
				}
			}
		});
	}
	
	private Runnable loadMoreProperty = new Runnable() {
		
		@Override
		public void run() {
			loadingMore = true;
			IWireFrameService service = WireFrameServiceFactory.getInstance().getService();
			List<Property> properties = null;
			String urlSearch = url+page+"&itemsperpage=10";
			try {
				properties =service.getSearchProperty(urlSearch);;
			} catch (Exception e) {
				e.printStackTrace();
			}
			Message msg = handlerSearch.obtainMessage();
			msg.obj = properties;
			handlerSearch.sendMessage(msg);
		}
	};
	private Handler handlerSearch = new Handler(){
		public void handleMessage (Message msg){
			dismissProgressDlg();

			if(msg.obj!=null){
				loadingMore = false;
				gView.setVisibility(View.VISIBLE);
				error.setVisibility(View.GONE);
				adapter.addProperties((List<Property>) msg.obj);
				adapter.notifyDataSetChanged();
				page++;
				loadingMore = false;
			}
			else if (loadingMore == false&&msg.obj==null) {
				gView.setVisibility(View.GONE);
				error.setVisibility(View.VISIBLE);
			}
		}
	};
	
}
